package com.grasp.netsdk;

import java.util.Set;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.grasp.base.ConstantBase;
import com.grasp.base.Logger;
import com.grasp.data.DeviceItem;

import cc.wulian.ihome.wan.MessageCallback;
import cc.wulian.ihome.wan.NetSDK;
import cc.wulian.ihome.wan.entity.DeviceEPInfo;
import cc.wulian.ihome.wan.entity.DeviceInfo;
import cc.wulian.ihome.wan.entity.GatewayInfo;

public class NetSDKCallBack implements MessageCallback {
	private Handler mHandler = null;

	public NetSDKCallBack(Handler handler) {
		mHandler = handler;
		System.out.println("===>initHandler: "+handler.getClass().getName());
	}

	/**
	 * {@link NetSDK#init(MessageCallback)}
	 * <p>
	 * 程序首次要调用 init 初始化SDK(返回值0为成功,-1为失败),然后再调用{@link NetSDK#connect()}连接服务器
	 * <p>
	 * {@link NetSDK#connect()}回调
	 * 
	 * @return 0 连接成功 -1 连接失败
	 */
	@Override
	public void ConnectServer(int result) {
		Logger.e("===>callBack ConnectServer:" + result);
		Message msg = new Message();
		msg.what = ConstantBase.CONNECT_SERVER;
		msg.arg1 = result;
		mHandler.sendMessage(msg);
	}

	/**
	 * {@link NetSDK#sendConnectGwMsg(appID, appType, gwID, gwPwd)}(连接网关)
	 * <p>
	 * appID 终端ID,appType终端类型, gwID网关ID, gwPwd网关密码 gwPwd要用MID5Util.encrypt方法加密
	 * <p>
	 * appType值分别为 0： PC 1：AndroidPhone 2：androidPad 3： Iphone 4： IPad
	 * 5：windowPhone 6：WindowPad
	 * 
	 * @param result
	 *            0成功 -1失败 11 网关不在线 12网关ID错误 13 网关密码错误 14 网关在其它服务器
	 *            ,当result为14是需要获取网关所在的服务器并重新连接
	 * @param gwID
	 *            网关ID
	 * @param gwInfo
	 *            网关信息包装类
	 * 
	 */
	@Override
	public void ConnectGateway(int result, String gwID, GatewayInfo gwInfo) {
		Logger.e("===>callBack ConnectGateway_result:" + result + ", gwID:"
				+ gwID + ", InfoData:" + gwInfo.getData());
		Message msg = new Message();
		msg.what = ConstantBase.CONNECT_SERVER_GATEWAY;
		Bundle bundle = new Bundle();
		bundle.putInt(ConstantBase.RESULT, result);
		bundle.putString(ConstantBase.GATEWAY, gwID);
		msg.setData(bundle);
		msg.obj = gwInfo;
		mHandler.sendMessage(msg);
	}

	/**
	 * {@link NetSDK#sendRefreshDevListMsg(gwID, null)}(获取网关设备列表)
	 * <p>
	 * 
	 * @param devInfo
	 *            包含了设备基本信息不含端口
	 * @param mDeviceEPInfos
	 *            包含了所有端口下的设备信息
	 */
	@Override
	public void DeviceUp(DeviceInfo devInfo, Set<DeviceEPInfo> mDeviceEPInfos) {
		if(mDeviceEPInfos.size()==0){
			
		}
		for(DeviceEPInfo deviceEPInfo : mDeviceEPInfos){
			Message msg = new Message();
			msg.what = ConstantBase.GET_DEVICE_LIST;
			DeviceItem item = new DeviceItem();
			item.setDeviceInfo(devInfo, deviceEPInfo);
			msg.obj = item;
			mHandler.sendMessage(msg);
		}
		Logger.e("===>callBack DeviceUp:" + devInfo.getDevID() + ", size()"+mDeviceEPInfos.size());
		System.out.println("===>devInfo========start");
		System.out.println("===>devInfo_getDevID:" + devInfo.getDevID());
		System.out.println("===>devInfo_getName:" + devInfo.getName());
		System.out.println("===>devInfo_getType:" + devInfo.getType());
		System.out.println("===>devInfo_getData:" + devInfo.getData());
		System.out.println("===>devInfo_getCategory:" + devInfo.getCategory());
		System.out.println("===>devInfo_getRoomID:" + devInfo.getRoomID());
		System.out.println("===>devInfo========end");
		for (DeviceEPInfo deviceEPInfo : mDeviceEPInfos) {
			System.out.println("===>deviceEPInfo========start===");
			System.out.println("===>getEp: " + deviceEPInfo.getEp());
			System.out.println("===>getEpAttr:" + deviceEPInfo.getEpAttr());
			System.out.println("===>getEpClst:" + deviceEPInfo.getEpClst());
			System.out.println("===>getEpName:" + deviceEPInfo.getEpName());
			System.out.println("===>getEpData:" + deviceEPInfo.getEpData());
			System.out.println("===>getEpStatus:" + deviceEPInfo.getEpStatus());
			System.out.println("===>getEpType: " + deviceEPInfo.getEpType());
			System.out.println("===>deviceEPInfo========end===");
		}
	}

	/**
	 * @param gwID
	 *            网关ID
	 * @param devID
	 *            设备ID
	 */
	@Override
	public void DeviceDown(String gwID, String devID) {
		Logger.e("===>callBack DeviceDown->gwID:" + gwID + ", devID:" + devID);
		Message msg = new Message();
		msg.what = ConstantBase.ERROR;
		msg.obj = "DeviceDown："+devID;
		mHandler.sendMessage(msg);
	}

	/**
	 * {@link NetSDK#sendControlDevMsg(gwID, devID, ep, epType, data)} (控制设备)
	 * <p>
	 * gwID 网关ID, devID 设备ID, Ep 设备端口, epType 设备类型, data 设备指令(参考开放协议的设备数据协议)
	 * 
	 * @param gwID
	 *            网关ID
	 * @param devID
	 *            设备ID
	 * @param devEPInfo
	 *            设备端口信息
	 */
	@Override
	public void DeviceData(String gwID, String devID, DeviceEPInfo devEPInfo) {
		
		Message msg = new Message();
		msg.what = ConstantBase.CONTROL_DEVICE;
		Bundle bundle = new Bundle();
		bundle.putString(ConstantBase.GATEWAY, gwID);
		bundle.putString(ConstantBase.DEVICE_ID, devID);
		msg.obj = devEPInfo;
		mHandler.sendMessage(msg);
		Logger.e("===>callBack DeviceData->gwID:" + gwID + ", devID:" + devID+",getEpData:" + devEPInfo.getEpData()
				+ ", getType: " + devEPInfo.getEpType() + ", getEp:"
				+ devEPInfo.getEp());
		System.out.println();
	}

	/**
	 * {@link NetSDK#sendDisConnectGwMsg(gwID)}(断开网关)
	 * <p>
	 * gwID网关ID 断开网关回调
	 * 
	 * @param result
	 * @param gwID
	 */
	@Override
	public void DisConnectGateway(int result, String gwID) {
		Logger.e("===>callBack DisConnectGateway->result:" + result + ", gwID:"
				+ gwID);
		Message msg = new Message();
		msg.what = ConstantBase.DIS_CONNECT_SERVER_GATEWAY;
		Bundle bundle = new Bundle();
		bundle.putInt(ConstantBase.RESULT, result);
		bundle.putString(ConstantBase.GATEWAY, gwID);
		mHandler.sendMessage(msg);
		
	}

	/**
	 * {@link NetSDK#sendChangeGwPwdMsg(gwID, oldPwd, newPwd)}(修改密码)
	 * 
	 * @param result
	 *            连接搜索结果
	 * @param gwID
	 *            网关ID
	 */
	@Override
	public void GatewayData(int result, String gwID) {
		Message msg = new Message();
		msg.what = ConstantBase.CHANGE_USER_PASSWORD;
		Bundle bundle = new Bundle();
		bundle.putInt(ConstantBase.RESULT, result);
		bundle.putString(ConstantBase.GATEWAY, gwID);
		mHandler.sendMessage(msg);
		Logger.e("===>callBack GatewayData->result:" + result + ", gwID:"
				+ gwID);
	}

	@Override
	public void GatewayDown(String result) {
		Logger.e("===>callBack GatewayDown->result:" + result);
		Message msg = new Message();
		msg.what = ConstantBase.ERROR;
		msg.obj = "GatewayDown："+result;
		mHandler.sendMessage(msg);
	}

	@Override
	public void HandleException(String result, Exception e) {
		Logger.e("===>callBack HandleException->result:" + result);
		e.printStackTrace();
		Message msg = new Message();
		msg.what = ConstantBase.ERROR;
		msg.obj = "连接异常："+result;
		mHandler.sendMessage(msg);
	}

}
